package com.yiren.dbaa.module.practice_set.map.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.model.RoadMarkModel;

import java.util.List;


/**
 * Describe 道路标识适配器
 */
public class RoadMarkAdapter extends BaseAdapter {

    private Context context;
    private List<RoadMarkModel> roadMarks;
    private OnMarkCollectClickListener listener;

    public RoadMarkAdapter(Context context, List<RoadMarkModel> roadMarks, OnMarkCollectClickListener listener) {
        this.context = context;
        this.roadMarks = roadMarks;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return roadMarks.size();
    }

    @Override
    public Object getItem(int position) {
        return roadMarks.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_road_mark, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        final Integer markType = roadMarks.get(position).getMarkType();
        switch (markType) {
            case 10:    //路口直行标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_zx);
                mHolder.tvMarkName.setText("路口直行标识");
                break;
            case 11:    //路口右转标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_yz);
                mHolder.tvMarkName.setText("路口右转标识");
                break;
            case 12:    //路口左转标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_zz);
                mHolder.tvMarkName.setText("路口左转标识");
                break;
            case 13:    //路口掉头标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_dt);
                mHolder.tvMarkName.setText("路口掉头标识");
                break;
            case 14:    //路口直行左转标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_zxzz);
                mHolder.tvMarkName.setText("路口直行左转标识");
                break;
            case 15:    //路口直行右转标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_zxyz);
                mHolder.tvMarkName.setText("路口直行右转标识");
                break;
            case 16:    //路口直行掉头标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_zxdt);
                mHolder.tvMarkName.setText("路口直行掉头标识");
                break;
            case 17:    //路口左转掉头标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_zzdt);
                mHolder.tvMarkName.setText("路口左转掉头标识");
                break;
            case 18:    //路口直行左转掉头标识
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_zxzzdt);
                mHolder.tvMarkName.setText("路口直行左转掉头标识");
                break;
            case 21:
                mHolder.ivMarkIcon.setImageResource(R.mipmap.mark_zyz);
                mHolder.tvMarkName.setText("路口左右转标识");
                break;
        }
        int point1Type = roadMarks.get(position).getPoint1Type();
        int point2Type = roadMarks.get(position).getPoint2Type();
        if (point1Type == 1) {
            mHolder.tvAction1.setText("采集点1");
            mHolder.tvAction1.setTextColor(context.getResources().getColor(R.color.main_blue));
        } else if (point1Type == 2) {
            mHolder.tvAction1.setText("校准点位1");
            mHolder.tvAction1.setTextColor(context.getResources().getColor(R.color.main_gray));
        } else {
            mHolder.tvAction1.setText("采集点1");
            mHolder.tvAction1.setTextColor(context.getResources().getColor(R.color.main_gray));
        }
        if (point2Type == 1) {
            mHolder.tvAction2.setText("采集点2");
            mHolder.tvAction2.setTextColor(context.getResources().getColor(R.color.main_blue));
        } else if (point2Type == 2) {
            mHolder.tvAction2.setText("校准点位2");
            mHolder.tvAction2.setTextColor(context.getResources().getColor(R.color.main_gray));
        } else {
            mHolder.tvAction2.setText("采集点2");
            mHolder.tvAction2.setTextColor(context.getResources().getColor(R.color.main_gray));
        }
        //道路标识
        mHolder.llRoadMark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onMarkCollectClick(markType, position, 0);
            }
        });
        //采集点1
        mHolder.tvAction1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (roadMarks.get(position).getPoint1Type() != 3) {
                    listener.onMarkCollectClick(markType, position, 1);
                }
            }
        });
        //采集点2
        mHolder.tvAction2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (roadMarks.get(position).getPoint2Type() != 3) {
                    listener.onMarkCollectClick(markType, position, 2);
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        LinearLayout llRoadMark;
        ImageView ivMarkIcon;
        TextView tvMarkName;
        TextView tvAction1;
        TextView tvAction2;

        public ViewHolder(View itemView) {
            llRoadMark = (LinearLayout) itemView.findViewById(R.id.ll_road_mark);
            ivMarkIcon = (ImageView) itemView.findViewById(R.id.iv_mark_icon);
            tvMarkName = (TextView) itemView.findViewById(R.id.tv_mark_name);
            tvAction1 = (TextView) itemView.findViewById(R.id.tv_action_1);
            tvAction2 = (TextView) itemView.findViewById(R.id.tv_action_2);
        }
    }

    public interface OnMarkCollectClickListener {
        void onMarkCollectClick(int markType, int position, int action);
    }
}
